//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hustcad.plm.pdm.form.controller;

import com.github.pagehelper.PageInfo;
import com.hustcad.plm.basic.annotion.TyFormCustomInterface;
import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.pdm.audit.util.TyplmAuditLogUtil;
import com.hustcad.plm.pdm.container.model.dto.DeleteTemplateDTO;
import com.hustcad.plm.pdm.form.model.dto.FormPageDTO;
import com.hustcad.plm.pdm.form.model.dto.FormTemplateDTO;
import com.hustcad.plm.pdm.form.model.dto.FormTemplateViewParamDTO;
import com.hustcad.plm.pdm.form.model.vo.FormTemplateVO;
import com.hustcad.plm.pdm.form.model.vo.FormTemplateViewVO;
import com.hustcad.plm.pdm.form.service.TyplmFormTemplateService;
import com.jsyxdl.utils.FormTemplateAndDataUtils;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.audit.AuditLogDO;
import com.ty.basic.entity.form.FormTemplateDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.utils.PageSizeUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/rest/v1/formTemplate"})
@TyFormCustomInterface
public class FormTemplateController {
    @Resource
    private TyplmFormTemplateService typlmFormTemplateService;

    public FormTemplateController() {
    }

    @PostMapping({"/createFormTemplate"})
    @ApiOperation(
            value = "创建表单模版",
            notes = "创建表单模版",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateDO> createFormTemplate(@RequestBody FormTemplateDTO formTemplateDTO) {
        try {
            FormTemplateDO formTemplateDO = this.typlmFormTemplateService.createFormTemplate(formTemplateDTO);
            if (formTemplateDO != null) {
                TyplmAuditLogUtil.info(formTemplateDO, LogEventEnum.CRATE_OBJECT, "");
            }

            return ResponseResult.success(formTemplateDO);
        } catch (Exception var4) {
            AuditLogDO auditLogDO = new AuditLogDO();
            auditLogDO.setObjotype("ty.inteplm.form.CTyFormTemplate");
            auditLogDO.setTargetname(formTemplateDTO.getName());
            auditLogDO.setContaineroid(formTemplateDTO.getContaineroid());
            auditLogDO.setContainerotype(formTemplateDTO.getContainerotype());
            auditLogDO.setLifecyclestageoid(formTemplateDTO.getLifecyclestageoid());
            auditLogDO.setLifecyclestageotype(formTemplateDTO.getLifecyclestageotype());
            auditLogDO.setLifecyclestatekey(auditLogDO.getLifecyclestatekey());
            TyplmAuditLogUtil.errorInfo(LogEventEnum.CRATE_OBJECT, auditLogDO, var4);
            throw var4;
        }
    }

    @PostMapping({"/updateFormTemplate"})
    @ApiOperation(
            value = "更新表单模版",
            notes = "更新表单模版",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateDO> updateFormTemplate(@RequestBody FormTemplateDTO formTemplateDTO) {
        try {
            FormTemplateDO formTemplateDO = this.typlmFormTemplateService.updateFormTemplate(formTemplateDTO);
            if (formTemplateDO != null) {
                TyplmAuditLogUtil.info(formTemplateDO, LogEventEnum.UPDATE_OBJECT, "");
            }

            return ResponseResult.success(formTemplateDO);
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(formTemplateDTO.getOid(), "ty.inteplm.form.CTyFormTemplate", LogEventEnum.UPDATE_OBJECT, var3);
            throw var3;
        }
    }

    @PostMapping({"/saveAsFormTemplate"})
    @ApiOperation(
            value = "另存为表单模版",
            notes = "另存为表单模版",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateDO> saveAsFormTemplate(@RequestBody FormTemplateDTO formTemplateDTO) {
        try {
            FormTemplateDO formTemplateDO = this.typlmFormTemplateService.saveAsFormTemplate(formTemplateDTO);
            if (formTemplateDO != null) {
                TyplmAuditLogUtil.info(formTemplateDO, LogEventEnum.CRATE_OBJECT, "");
            }

            return ResponseResult.success(formTemplateDO);
        } catch (Exception var4) {
            AuditLogDO auditLogDO = new AuditLogDO();
            auditLogDO.setObjotype("ty.inteplm.form.CTyFormTemplate");
            auditLogDO.setTargetname(formTemplateDTO.getName());
            auditLogDO.setContaineroid(formTemplateDTO.getContaineroid());
            auditLogDO.setContainerotype(formTemplateDTO.getContainerotype());
            auditLogDO.setLifecyclestageoid(formTemplateDTO.getLifecyclestageoid());
            auditLogDO.setLifecyclestageotype(formTemplateDTO.getLifecyclestageotype());
            auditLogDO.setLifecyclestatekey(auditLogDO.getLifecyclestatekey());
            TyplmAuditLogUtil.errorInfo(LogEventEnum.CRATE_OBJECT, auditLogDO, var4);
            throw var4;
        }
    }

    @PostMapping({"/deleteFormTemplate"})
    @ApiOperation(
            value = "删除表单模版",
            notes = "删除表单模版",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult deleteFormTemplate(@RequestBody DeleteTemplateDTO deleteTemplateDTO) {
        try {
            TyplmAuditLogUtil.info(deleteTemplateDTO.getTemplateMasterOid(), "ty.inteplm.form.CTyFormTemplate", LogEventEnum.DELETE_OBJECT);
            this.typlmFormTemplateService.deleteFormTemplate(deleteTemplateDTO);
            return ResponseResult.success();
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(deleteTemplateDTO.getTemplateMasterOid(), "ty.inteplm.form.CTyFormTemplate", LogEventEnum.DELETE_OBJECT, var3);
            throw var3;
        }
    }

    @PostMapping({"/queryFormTemplate"})
    @ApiOperation(
            value = "分页查询表单模板",
            notes = "分页查询表单模板",
            produces = "application/json; charset=utf-8"
    )
    @TyFormCustomInterface(
            restfulUrl = "/rest/v1/formTemplate/queryFormTemplate",
            returnValueClass = FormTemplateVO.class,
            category = "表单",
            name = "分页查询表单模板",
            description = "分页查询表单模板",
            requestType = "POST"
    )
    public ResponseResult<PageInfo<FormTemplateVO>> queryFormTemplate(@RequestBody FormPageDTO formPageDTO) {
        formPageDTO.setPageSize(PageSizeUtil.getNormalSize(formPageDTO.getPageSize()));
        return ResponseResult.success(this.typlmFormTemplateService.queryFormTemplatePage(formPageDTO));
    }

    @PostMapping({"/queryFormTemplateList"})
    @ApiOperation(
            value = "类型初始化规则绑定表单模板时获取表单模板列表",
            notes = "类型初始化规则绑定表单模板时获取表单模板列表",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<PageInfo<FormTemplateVO>> queryFormTemplateList(@RequestBody FormPageDTO formPageDTO) {
        formPageDTO.setPageSize(PageSizeUtil.getNormalSize(formPageDTO.getPageSize()));
        return ResponseResult.success(this.typlmFormTemplateService.queryFormTemplateList(formPageDTO));
    }

    @PostMapping({"/saveFormDataDesign"})
    @ApiOperation(
            value = "保存表单设计",
            notes = "保存表单设计",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult saveFormDataDesign(@RequestBody FormTemplateDTO formTemplateDTO) {
        try {
            this.typlmFormTemplateService.saveFormDataDesign(formTemplateDTO);
            return ResponseResult.success();
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(formTemplateDTO.getOid(), "ty.inteplm.form.CTyFormTemplate", LogEventEnum.UPDATE_OBJECT, var3);
            throw var3;
        }
    }

    @PostMapping({"/iterateFormDataDesign"})
    @ApiOperation(
            value = "保存表单设计并检入",
            notes = "检出表单模板并保存表单设计并检入",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult iterateFormDataDesign(@RequestBody FormTemplateDTO formTemplateDTO) {
        try {
            this.typlmFormTemplateService.iterateFormDataDesign(formTemplateDTO);
            return ResponseResult.success();
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(formTemplateDTO.getOid(), "ty.inteplm.form.CTyFormTemplate", LogEventEnum.CHECK_IN, var3);
            throw var3;
        }
    }

    @PostMapping({"/checkOutFormTemplate"})
    @ApiOperation(
            value = "检出表单模板",
            notes = "检出表单模板",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateVO> checkOutFormTemplate(@RequestBody BigInteger oid) {
        try {
            return ResponseResult.success(this.typlmFormTemplateService.checkOutFormTemplate(oid));
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(oid, "ty.inteplm.form.CTyFormTemplate", LogEventEnum.CHECK_OUT, var3);
            throw var3;
        }
    }

    @PostMapping({"/checkInFormTemplate"})
    @ApiOperation(
            value = "检入表单模板",
            notes = "检入表单模板",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateVO> checkInFormTemplate(@RequestBody BigInteger oid) {
        try {
            return ResponseResult.success(this.typlmFormTemplateService.checkInFormTemplate(oid, (String)null));
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(oid, "ty.inteplm.form.CTyFormTemplate", LogEventEnum.CHECK_IN, var3);
            throw var3;
        }
    }

    @PostMapping({"/undoCheckOutFormTemplate"})
    @ApiOperation(
            value = "撤销检出表单模板",
            notes = "撤销检出表单模板",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateVO> undoCheckOutFormTemplate(@RequestBody BigInteger oid) {
        try {
            return ResponseResult.success(this.typlmFormTemplateService.undoCheckOutFormTemplate(oid));
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(oid, "ty.inteplm.form.CTyFormTemplate", LogEventEnum.UNDO_CHECK_OUT, var3);
            throw var3;
        }
    }

    @PostMapping({"/enableFormTemplate"})
    @ApiOperation(
            value = "启用和停用模板信息",
            notes = "启用和停用模板信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult enableFormTemplate(@RequestBody FormTemplateDTO formTemplateDTO) {
        try {
            this.typlmFormTemplateService.enableFormTemplate(formTemplateDTO);
            return ResponseResult.success();
        } catch (Exception var3) {
            TyplmAuditLogUtil.errorInfo(formTemplateDTO.getOid(), "ty.inteplm.form.CTyFormTemplate", LogEventEnum.UPDATE_OBJECT, var3);
            throw var3;
        }
    }

    @PostMapping({"/getEnableFormTemplateByType"})
    @ApiOperation(
            value = "根据类型获取启用的模板定义",
            notes = "根据类型获取模板定义",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateVO> getEnableFormTemplateByType(@RequestBody IdentifierEntity identifierEntity) {
        return ResponseResult.success(this.typlmFormTemplateService.getEnableFormTemplateByType(identifierEntity));
    }

    @PostMapping({"/getFormTemplate"})
    @ApiOperation(
            value = "获取模板定义",
            notes = "获取模板定义",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateVO> getFormTemplate(@RequestBody BigInteger oid) {
        return ResponseResult.success(this.typlmFormTemplateService.getFormTemplate(oid));
    }

    @PostMapping({"/getFormTemplateByType"})
    @ApiOperation(
            value = "根据类型获取模板定义",
            notes = "根据类型获取模板定义",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateVO> getFormTemplateByType(@RequestBody Object json) {

        FormTemplateVO formTemplateVO=null;
        if(json instanceof Long){
            Long val= (Long) json;
            BigInteger oid=new BigInteger(val.toString());
            formTemplateVO=this.typlmFormTemplateService.getFormTemplateByType(oid);

        }
        else if(json instanceof LinkedHashMap){
            Map<String,Object> map= (Map<String, Object>) json;
            BigInteger oid=new BigInteger((String)map.get("oid"));
            formTemplateVO=typlmFormTemplateService.getFormTemplateByType(oid);
            FormTemplateAndDataUtils.getFormTemplateByTypeByFormData(formTemplateVO, json);
        }
        return ResponseResult.success(formTemplateVO);
    }

    @PostMapping({"/getLatestFormTemplate"})
    @ApiOperation(
            value = "查询最新表单模板",
            notes = "查询最新表单模板",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<FormTemplateVO>> getLatestFormTemplateByMasterOidList(@RequestBody List<BigInteger> masterOids) {
        return ResponseResult.success(this.typlmFormTemplateService.getLatestFormTemplateByMasterOidList(masterOids));
    }

    @PostMapping({"/getFormTemplateViewDetail"})
    @ApiOperation(
            value = "查询最新表单模板",
            notes = "查询最新表单模板",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateViewVO> getFormTemplateViewDetail(@RequestBody FormTemplateViewParamDTO paramDTO) {
        return ResponseResult.success(this.typlmFormTemplateService.getFormTemplateViewDetail(paramDTO));
    }

    @PostMapping({"/getFormTemplateViewDetailByType"})
    @ApiOperation(
            value = "根据类型获取模板详情",
            notes = "根据类型获取模板详情",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<FormTemplateViewVO> getFormTemplateViewDetailByType(@RequestBody FormTemplateViewParamDTO paramDTO) {
        return ResponseResult.success(this.typlmFormTemplateService.getFormTemplateViewDetailByType(paramDTO));
    }
}
